#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //对象实例化
    mediaPlayer = new QMediaPlayer(this);
    videoWidget = new QVideoWidget(this);

    //设置播放画⾯的窗⼝
    videoWidget->setMinimumSize(800,600);

    //实例化窗⼝布局---垂直布局
    this->vboxloyout = new QVBoxLayout(this);
    this->setLayout(this->vboxloyout);

    //实例化选择视频按钮
    chooseButton = new QPushButton("选择视频",this);
    //实例化播放按钮
    playButton = new QPushButton(this);
    //设置图标代替⽂件
    playButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));

    //设置布局
    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addWidget(chooseButton);
    hbox->addWidget(playButton);
    vboxloyout->addWidget(videoWidget);
    vboxloyout->addLayout(hbox);

    //将选择视频对应的按钮和槽函数进⾏关联
    connect(chooseButton,&QPushButton::clicked,this,&Widget::chooseVideo);
}

void Widget::chooseVideo()
{
    //选择视频，返回⼀个播放视频的名字
    QString name = QFileDialog::getSaveFileName(this,"选择视频",".","WMV(*.wmv)");
    //设置媒体声⾳
    mediaPlayer->setMedia(QUrl(name));
    //输出视频画⾯
    mediaPlayer->setVideoOutput(videoWidget);
    //播放
    mediaPlayer->play();
}

Widget::~Widget()
{
}

